﻿using Report_Dal.Web;
using Report_IDal.Basic;
using Report_IDal.Web;
using Report_Model.Db;
using Report_Unity.Basic;
using Report_Unity.Unitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace Report_Service.Basic
{
    public abstract class BaseService<T> where T : class, new()
    {
        public IBaseDal<T> BaseDal { get; set; }
        public abstract void SetCurrentDal();
        public BaseService()
        {
            SetCurrentDal();
        }

        public IQueryable<T> Get(Expression<Func<T, bool>> whereLambda)
        {
            return BaseDal.Get(whereLambda);
        }



        public IQueryable<T> GetAsNoTracking(Expression<Func<T, bool>> whereLambda)
        {
            return BaseDal.GetAsNoTracking(whereLambda);
        }

        public IQueryable<T> GetPage<s>(int pageIndex, int pageSize, out int totalCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, s>> orderLambda, bool flag = true)
        {
            return BaseDal.GetPage<s>(pageIndex, pageSize, out totalCount, whereLambda, orderLambda, flag);
        }

        public bool Add(T t)
        {
            return BaseDal.Add(t);
        }

        public bool AddList(List<T> list)
        {
            return BaseDal.AddList(list);
        }

        public bool Up(T t)
        {
            return BaseDal.Up(t);
        }

        public bool UpList(List<T> list)
        {
            return BaseDal.UpList(list);
        }

        public bool UpArray(params T[] ts)
        {
            return BaseDal.UpArray(ts);
        }

        public bool Del(T t)
        {
            return BaseDal.Del(t);
        }

        public bool DelList(List<T> list)
        {
            return BaseDal.DelList(list);
        }

        public bool SaveChange()
        {
            return BaseDal.SaveChanges();
        }

        public List<T> GetSelectIn(Expression<Func<T, string>> keyField, List<string> list)
        {
            return BaseDal.GetSelectIn(keyField, list);
        }


        public int GetNextSort(Expression<Func<T, bool>> whereLambda, Expression<Func<T, int>> selector)
        {
            return BaseDal.GetNextSort(whereLambda, selector);
        }

        /// <summary>
        /// 上下移动
        /// </summary>
        /// <param name="whereLambda">排序的范围</param>
        /// <param name="whereid">当前操作的值</param>
        /// <param name="keySelector">排序的字段</param>
        /// <param name="dirc">方向</param>
        /// <returns></returns>
        public ResultInfo MoveObj(Expression<Func<T, bool>> whereLambda, Func<T, bool> whereid, Expression<Func<T, int>> keySelector, string dirc)
        {
            return BaseDal.MoveObj(whereLambda, whereid, keySelector, dirc);
        }

        public string GetNextId(Expression<Func<T, string>> fnnc, bool year = false, bool month = false, bool day = false, int num = 7)
        {
            return BaseDal.GetNextId(fnnc, year, month, day, num);
        }

        public long GetNextId(Expression<Func<T, long>> fnnc)
        {
            return BaseDal.GetNextId(fnnc);
        }

        public int GetNextId(Expression<Func<T, int>> fnnc)
        {
            return BaseDal.GetNextId(fnnc);
        }

        public ResultInfo GetLoginTicket(string hid)
        {
            if (string.IsNullOrEmpty(hid))
                return CodeStatus.error_201.GetStatus($"{nameof(hid)}必传");
            Guid ticket = IDGeneratorUtils.NewGuid();
            StringBuilder sbCode = new StringBuilder();
            Random r = new Random();
            for (int i = 0; i < 4; i++)
            {
                sbCode.Append(r.Next(0, 9));
            }
            string code = sbCode.ToString();
            byte[] bys = ValidateCodeUtils.CreateImage(code);
            string prefix = "data:image/png;base64,";
            string base64Img = prefix + Convert.ToBase64String(bys);

            T_Ticket_Record record = new T_Ticket_Record();
            record.check_ticket = ticket;
            record.check_code = code;
            record.hospital_id = hid;
            record.create_time = DateTime.Now;
            IT_Ticket_RecordDal Ticket_RecordDal = new T_Ticket_RecordDal(ConfigSource.tab_web);
            if (!Ticket_RecordDal.Add(record))
                return CodeStatus.error_301.GetStatus("创建失败");

            ResultInfo res = CodeStatus.success.GetStatus();
            res.data = new { ticket = ticket, img_code = base64Img };
            return res;
        }
    }
}
